<?php


namespace App\Tools;


use App\Models\MerchantShop;
use App\Models\PushTask;
use App\Models\ShopGroup;
use App\Models\SystemConfig;
use Illuminate\Support\Facades\Redis;

class Repeats
{
    /**
     * 查询用户是否在商户有复购情况
     * @param $isTbOrJd
     * @param $merchant_id
     * @param $push_id
     * @return bool
     */
    public function findMerchant($isTbOrJd, $merchant_id, $push_id)
    {
        $return = false;
        $pushTask = PushTask::where('merchant_id', $merchant_id);
        $pushTask->where('task_type', 1);
        $pushTask->where('push_id', $push_id);
        $pushTask->whereIn('status', [6, 7, 8]);
        if (in_array($this->tasks['platform_type'], [1, 2, 4])) {
            $pushTask->whereIn('shop_type', [1, 2, 4]);
        }else{
            $pushTask->where('shop_type', $this->tasks['platform_type']);
        }
        // 同商品限制
        $days = SystemConfig::findValue($isTbOrJd ? 'posputplan.pos1' : 'posputplan.pos2');
        if ($days) {
            $st = time() - ($days * 24 * 60 * 60);
            $et = time();
            $pushTask->whereBetween('create_time', [$st, $et]);
            $result = $pushTask->first();
            if (!empty($result)) {
                $return = true;
            }
        }
        return $return;
    }

    /**
     * 查询用户是否在店铺有复购情况
     * @param $isTbOrJd
     * @param $shop_id
     * @param $push_id
     * @return bool
     */
    public function findShop($isTbOrJd, $shop_id, $push_id)
    {
        $return = false;
        $pushTask = PushTask::where('shop_id', $shop_id);
        $pushTask->where('task_type', 1);
        $pushTask->where('push_id', $push_id);
        $pushTask->whereIn('status', [6, 7, 8]);
        // 同商铺限制
        $days = SystemConfig::findValue($isTbOrJd ? 'posputplan.shop1' : 'posputplan.shop2');
        if ($days) {
            $st = time() - ($days * 24 * 60 * 60);
            $et = time();
            $pushTask->whereBetween('create_time', [$st, $et]);
            $result = $pushTask->first();
            if (!empty($result)) {
                $return = true;
            }
        }
        return $return;
    }

    /**
     * 查询用户是否在店铺组有复购情况
     * @param $shop_id
     * @param $push_id
     * @return bool
     */
    public function findShopGroup($shop_id, $push_id)
    {
        // 查询组ID
        $return = false;
        $group_id = MerchantShop::where('id', $shop_id)->value('group_id');
        if (isset($group_id)  && $group_id > 0) {
            $shopIds = MerchantShop::where('group_id', $group_id)->pluck('id');
            $pushTask = PushTask::whereIn('shop_id', $shopIds);
            $pushTask->where('push_id', $push_id);

            $days = ShopGroup::where('id', $group_id)->value('days');
            if ($days) {
                $st = time() - ($days * 24 * 60 * 60);
                $et = time();
                $pushTask->whereBetween('create_time', [$st, $et]);
            }
            $result = $pushTask->first();
            if (!empty($result)) {
                $return = true;
            }
        }
        return $return;
    }

    /**
     * 查询用户是否在同商品有复购情况
     * @param $isTbOrJd
     * @param $goods_id
     * @param $push_id
     * @return bool
     */
    public function findGoods($isTbOrJd, $goods_id, $push_id)
    {
        $return = false;
        $pushTask = PushTask::where('goods_id', $goods_id);
        // 订单类型
        $pushTask->where('task_type', 1);
        $pushTask->where('push_id', $push_id);
        // 是否派给领取后放弃的用户
        if (SystemConfig::findValue('dispatch.togiveupuser', 1) == 1) {
            $pushTask->whereIn('status', [6, 7, 8]);
        }else{
            $pushTask->whereIn('status', [7, 6, 8, 3]);
        }
        // 同商品限制天数
        $limitDays = SystemConfig::findValue($isTbOrJd ? 'posputplan.good1' : 'posputplan.good2');
        if ($limitDays) {
            $st = time() - ($limitDays * 24 * 60 * 60);
            $et = time();
            $pushTask->whereBetween('create_time', [$st, $et]);
            $result = $pushTask->first();
            if (!empty($result)) {
                $return = true;
            }
        }
        return $return;
    }
}
